package org.chatboleh;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.apache.log4j.Logger;
import org.chatboleh.bean.User;
import org.chatboleh.controller.ApplicationFrameController;
import org.chatboleh.frame.internal.DocumentList;
import org.chatboleh.frame.internal.LoginFrame;
import org.chatboleh.frame.internal.PayeeList;
import org.dyno.visual.swing.layouts.Bilateral;
import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;

//VS4E -- DO NOT REMOVE THIS LINE!
public class ApplicationFrame extends JFrame {
	private static Logger logger = Logger.getLogger(ApplicationFrame.class);
	private static final long serialVersionUID = 1L;
	private static ApplicationFrameController CONTROLLER;
	private JDesktopPane jDesktopPane0;
//	private JMenuBar menuBar;
	// private JMenu menu, submenu;
	private LoginFrame loginframe;
	private PayeeList jPayeeList;
	private DocumentList jDocumentList;
	private JMenuItem jMenuItem0;
	private JMenu jMenu0;
	private JMenuBar jMenuBar1;
	public User user = null;
	private static final String PREFERRED_LOOK_AND_FEEL = "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel";

	public ApplicationFrame() {
		CONTROLLER = new ApplicationFrameController(this);
		initComponents();
	}

	private void initComponents() {
		setLayout(new GroupLayout());
		add(getJDesktopPane0(), new Constraints(new Bilateral(0, 0, 0),
				new Bilateral(0, 0, 0)));
		addWindowStateListener(new WindowStateListener() {
			@Override
			public void windowStateChanged(WindowEvent event) {
				windowStateWindowStateChanged(event);
			}
		});
		addWindowListener(CONTROLLER.new WindowEventAdapter()); 
		setJMenuBar(getJMenuBar1());
		setSize(800, 600);
		setLocationRelativeTo(null);
	}

	public JMenuBar getJMenuBar1() {
		if (jMenuBar1 == null) {
			jMenuBar1 = new JMenuBar();
			jMenuBar1.add(getJMenu0());
		}
		return jMenuBar1;
	}

	public JMenu getJMenu0() {
		if (jMenu0 == null) {
			jMenu0 = new JMenu();
			jMenu0.setMnemonic('F');
			jMenu0.setText("File");
			jMenu0.setOpaque(false);
			jMenu0.add(getJMenuItem0());
		}
		return jMenu0;
	}

	private JMenuItem getJMenuItem0() {
		if (jMenuItem0 == null) {
			jMenuItem0 = new JMenuItem();
			jMenuItem0.setMnemonic('X');
			jMenuItem0.setText("Exit");
			jMenuItem0.addMouseListener(CONTROLLER.new MenuExitMouseListener());
		}
		return jMenuItem0;
	}

	public JDesktopPane getJDesktopPane0() {
		if (jDesktopPane0 == null) {
			jDesktopPane0 = new JDesktopPane();
			jDesktopPane0.setBackground(Color.gray);
			jDesktopPane0.setDoubleBuffered(false);
			jDesktopPane0.setAutoscrolls(true);
		}
		return jDesktopPane0;
	}

	public void addChildFrame(Component child) {
		try {
			if (!child.isValid()) {
				logger.debug("Adding " + child.getName() + " to desktoppanel.");
				jDesktopPane0.add(child);
			}
			;
		} catch (Exception e) {
			// Donnot how this happen. But this is just a hack.
			logger.error("SHIT HAPPEN", e);
			jDesktopPane0.repaint();
			logger.debug("Readding " + child.getName() + " to desktoppanel.");
			jDesktopPane0.add(child);
		}
		child.setVisible(true);
		child.requestFocus();
	}

	private static void installLnF() {
		try {
			String lnfClassname = PREFERRED_LOOK_AND_FEEL;
			if (lnfClassname == null)
				lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
			UIManager.setLookAndFeel(lnfClassname);
		} catch (Exception e) {
			logger.error("Cannot install " + PREFERRED_LOOK_AND_FEEL
					+ " on this platform:" + e.getMessage());
		}
	}

	private JMenuItem getLogoutMenu() {
		JMenuItem item = new JMenuItem("Logout");
		item.addActionListener(CONTROLLER.new LogoutActionListener());
		return item;
	}

	public void ShowPayeeList() {
		if (jPayeeList == null) {
			jPayeeList = new PayeeList();
		}
		jPayeeList.setLocation(200, 0);
		addChildFrame(jPayeeList);
	}

	public void ShowDocumentList() {
		if (jDocumentList == null) {
			jDocumentList = new DocumentList();
		}
		addChildFrame(jDocumentList);
	}

	public void ShowUserMenu() {
		logger.debug("");
		getJMenuBar1().getMenu(0).add(getLogoutMenu(), 0);

		JMenu menu = new JMenu("View");
		menu.setMnemonic(KeyEvent.VK_V);
		getJMenuBar1().add(menu);

		JMenuItem menuItem = new JMenuItem("Payee List", KeyEvent.VK_P);
		menuItem.addActionListener(CONTROLLER.new PayeeListActionListener());
		menu.add(menuItem);
		
		JMenuItem documentListMenuItem = new JMenuItem("Document List", KeyEvent.VK_D);
		documentListMenuItem.addActionListener(CONTROLLER.new DocumentListActionListener());
		menu.add(documentListMenuItem);
		
	}

	public void ShowLoginFrame() {
		if (loginframe == null) {
			loginframe = new LoginFrame();
			loginframe.addComponentListener(CONTROLLER.new LoginFrameHiddenAdapter());
		}
		addChildFrame(loginframe);
		loginframe.setLocation(300,100);
	}

	private void windowStateWindowStateChanged(WindowEvent event) {
		System.out.println("STATE = " + event.getNewState());
	}

	/**
	 * Main entry of the class. Note: This class is only created so that you can
	 * easily preview the result at runtime. It is not expected to be managed by
	 * the designer. You can modify it as you like.
	 */
	public static void main(String[] args) {
		installLnF();
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				ApplicationFrame frame = new ApplicationFrame();
				frame.setDefaultCloseOperation(ApplicationFrame.EXIT_ON_CLOSE);
				frame.setTitle("ApplicationFrame");
				frame.getContentPane().setPreferredSize(frame.getSize());
				frame.pack();
				frame.setLocationRelativeTo(null);
				frame.ShowLoginFrame();
				frame.setVisible(true);
			}
		});
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

}
